<?php
require_once APP_PATH . '/page/model.php';
function page_create_action() {
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		//提交保存
		page_save_action();
	} else {
		//显示编辑页面
		page_show_action();
	}

}
/**
 * 显示page页面
 * @param  string
 * @return [type]
 */
function page_show_action() {
	checkLogin();
	$hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : '';

	if (empty($hash)) {
		include APP_ADMIN_VIEW . 'page/create.php';
	} else {
		$hash = filter_data($hash);
		//获取数据
		$page_data = page_model_get_by_hash($hash);
		//输出
		if ($page_data) {
			include APP_ADMIN_VIEW . 'page/create.php';
		} else {
			show_not_found();
		}
	}

}

function page_save_action() {
	checkLogin();
	$hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : '';
	$title = isset($_POST['title']) ? trim($_POST['title']) : '';
	$content = isset($_POST['content']) ? trim($_POST['content']) : '';
	$menu_id = isset($_POST['menu_id']) ? intval($_POST['menu_id']) : 0;
	$url = isset($_POST['url']) ? trim($_POST['url']) : '';
	$keywords = isset($_POST['keywords']) ? trim($_POST['keywords']) : '';
	$description = isset($_POST['description']) ? trim($_POST['description']) : '';
	$status = isset($_POST['status']) ? intval(trim($_POST['status'])) : 0;

	$draft = isset($_POST['draft']) ? intval(trim($_POST['draft'])) : 0;

	if (empty($hash)) {
		//新建
		//获取数据
		$ret = page_model_insert($url, $title, $menu_id, $content, $status, $draft, $keywords, $description);
		if ($ret) {
			header("Location: /" . APP_ADMIN_URI . "/page/list");
		} else {
			echo "insert error.";
		}
	} else {
		//更新
		//获取数据
		$ret = page_model_update_by_hash($url, $hash, $title, $menu_id, $content, $status, $draft, $keywords, $description);
		if ($ret) {
			header("Location: /" . APP_ADMIN_URI . "/page/list");
		} else {
			echo "update error.";
		}
	}

}